<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 11:06
 */

namespace app\api\service;

use app\api\model\Recharge;
use app\api\model\Recharge as RechargeModel;
use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use think\Db;

class Payment
{
    //支付成功处理==========
    public static function alipayApppaySuccess($param)
    {
        dump($param);
        $order_no = $param['out_trade_no'];
//        $order = Db::name('recharge');
//        $info = $order->find(['order_no' => $order_no]);
        $info = RechargeModel::get(['order_no' => $order_no]);
        //订单状态判断
        if ($info['status'] == 1 || $info['status'] == -1 || $info['status'] == 2) {
            echo 222;
            return;
        }
//        if ($info['amount'] != $param['total_amount']) {
        if (false) {
            //支付金额不正确
            //TODO
            $info->remark = '支付金额与订单金额不一致';
            $info->status = 2;//订单状态  异常
        } else {
            if ($info['type'] == 1) {
                //金币充值
                self::rechargeSuccess($info);
            } elseif ($info['type'] == 2) {
                //申请发包方
                self::initiatorSuccess($info);
            } elseif ($info['type'] == 3) {
                //申请推广员
                self::promoterSuccess($info);
            } elseif ($info['type'] == 4) {
                //充值月服务费
                self::memberSuccess($info);
            }
            $info->status = 1;//订单状态  成功
            $info->alipay_order_id = $param['trade_no'];
        }
        $info->save();
    }

    //申请脉大师
    private static function promoterSuccess($info)
    {
        $user_id = $info['user_id'];
        $user = UserModel::get($user_id);
        //支付成功,设为推广员,奖励上级直推推广员一份分红权
        $res = UserModel::update(['is_promoter' => 1], ['id' => $user_id]);
        if (!$res) {
            return json(['errorCode' => 1, 'msg' => '申请失败']);
        }
        //更改推荐关系表中是否脉大师状态
//        RecommendModel::update(['is_promoter' => 1], ['user_id' => $user_id]);
        //增加上级脉大师一份分红权重
        $promoter = UserModel::get($user['parentid']);
        if ($promoter['is_promoter'] == 1) {
            UserModel::where('id', $promoter['id'])->setInc('bonus_weight');
        }

        //用户分红
        UserService::rechargeReward($user_id, $info['amount'], '申请【脉大师】');

//        return json(['errorCode' => 0, 'msg' => '申请成功']);
    }

    //申请主编、小编
    private static function initiatorSuccess($info)
    {
        $user_id = $info['user_id'];
        $detail = json_decode($info['detail'], true);
        $give_gold = $detail['give_gold'];
        $level = $detail['level'];
        if ($level == 1) {
            $level_str = '小编';
        } elseif ($level == 2) {
            $level_str = '主编';
        }
        //金币日志
        $log_data = [
            'title' => '申请【' . $level_str . '】赠送',
            'user_id' => $user_id,
            'task_id' => '',
            'type' => 1,
            'amount' => $give_gold,
            'add_time' => time()
        ];
        Db::name('gold')->insert($log_data);
        //增加金币
//        $res = UserModel::where('id', $user_id)->setInc('gold', $give_gold);
        $user = UserModel::get($user_id);
        $user->gold = $user['gold'] + $give_gold;
        $user->level = $level;
        $res = $user->save();

        //冻结转余额
        UserService::frozenToMoney($user_id);

        //用户分红
        UserService::rechargeReward($user_id, $info['amount'], '申请【' . $level_str . '】');
    }

    //金币充值
    private static function rechargeSuccess($info)
    {
        $user_id = $info['user_id'];
        $detail = json_decode($info['detail'],true);
        $quantity = $detail['gold'];
        //金币日志
        $log_data = [
            'title' => '金币充值',
            'user_id' => $user_id,
            'task_id' => '',
            'type' => 1,
            'amount' => $quantity,
            'add_time' => time()
        ];
        Db::name('gold')->insert($log_data);
        //增加金币
        UserModel::where('id', $user_id)->setInc('gold', $quantity);

        //用户分红
        UserService::rechargeReward($user_id, $info['amount'], '充值');
    }

    //充值月服务费
    private static function memberSuccess($info){
        $user_id = $info['user_id'];
        $user = UserModel::get($user_id);
        $member_time = UserService::getMemberTime($user);
        $user->member_time = $member_time;
        $user->save();
    }

    //支付成功处理==========
}